<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        #app {
            padding: 30px 20px;
        }

        .title {
            display: flex;
            justify-content: space-between;
            margin: 15px;
        }

        .title input {
            width: 85%;
            height: 30px;
            border: 1px solid #ccc;
            text-indent: 20px;
            color: gray;
            background-color: rgb(238, 223, 223);
        }

        .title span {
            color: rgb(8, 114, 221);
            font-size: 20px;
            line-height: 30px;
        }

        .renwu {
            display: flex;
        }

        .renwu>span {
            padding: 10px;
            background-color: lightseagreen;
            margin: 10px;
            border-radius: 10px;
        }

        .renwu .active {
            background-color: red;
        }

        h2 {
            margin: 20px 0;
        }

        .one {
            display: flex;
            justify-content: space-between;
            background-color: red;
            margin-bottom: 20px;
        }

        .one input {
            text-align: center;
            margin-left: 10px;
        }

        .one span {
            padding: 15px;
        }

        .one .neirong {
            flex: 1;
        }

        .one.finished {
            background-color: lightseagreen;
        }

        .empty {
            margin: 50px 0;
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="title">
            <input type="text" placeholder="输入任务名称" v-model="innertext">
            <span @click="add">新增</span>
        </div>
        <div class="renwu">
            <span>全部任务({{list.length}})</span>
            <span>已完成({{finished}})</span>
            <span class="active">未完成({{unfinished}})</span>
        </div>
        <h2>任务列表</h2>
        <main>
            <div :class="{one:true,finished:item.status}" v-for="(item,index) in list" :key="index">
                <input type="checkbox" @click="changebox(item)">
                <span>{{item.count}}</span>
                <span class="neirong">{{item.neirong}}</span>
                <span @click="del(item)">删除</span>
            </div>
            <div class="empty" v-show="list.length == 0">任务列表空空如也!</div>
        </main>
    </div>
</body>

</html>
<script src="./js/vue-2.5.21.js"></script>
<script>
    const v = new Vue({
        el: '#app',
        data: {
            innertext: '',
            list: [],
            //深度监听需要加
            finished: 0,
            unfinished: 0
        },
        methods: {
            add() {//新增
                if (this.innertext) {
                    let obj = { id: this.list.length + 1, count: this.list.length + 1, neirong: this.innertext, status: false }
                    this.list.push(obj)
                    this.innertext = ''
                }
            },
            changebox(item) {//改变状态
                item.status = !item.status

            },
            // del(item, index) {//删除
            //     this.list.splice(index, 1)
            // }
            del(item) {//删除
                let i = this.list.findIndex((t) => {
                    return t.id == item.id
                })
                if (i != -1) {
                    this.list.splice(i, 1)
                }
            }
            // finished() {//已完成
            //     var count = 0
            //     this.list.forEach((item, index) => {
            //         if (item.status == true) {
            //             count++
            //         }
            //     })
            //     return count
            // },
            // unfinished() {//未完成
            //     var count = 0
            //     this.list.forEach((item, index) => {
            //         if (item.status == false) {
            //             count++
            //         }
            //     })
            //     return count
            // }
        },

        // computed: {//定义计算属性, 计算属性定义时像函数, 使用时像变量
        //     finished() {
        //         return this.list.filter((item) => { return item.status }).length;
        //     },
        //     unfinished() {
        //         return this.list.length - this.finished;
        //     },
        // },

        watch: {//定义数据监听, 可以监听数据的改变, 当被监听的数据发生改变时, 会执行对应的函数
            list: { //对象的形式+ 深度监听 可以监听到内部数据属性的改变
                handler() {
                    this.finished = this.list.filter((item) => { return item.status }).length;

                    this.unfinished = this.list.length - this.list.filter((item) => { return item.status }).length;
                },
                deep: true,
            }
        }


    })
</script>